package com.slz3.business.module.wmsManagement.wmsStockPut.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.wmsManagement.wmsStockPut.domain.WmsStockPut;
import com.slz3.business.module.wmsManagement.wmsStockPut.domain.WmsStockPutFactory;
import com.slz3.business.module.wmsManagement.wmsStockPut.steward.WmsStockPutSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
@Service
public class WmsStockPutServiceImpl implements WmsStockPutService {
    @Override
    public WmsStockPut save(WmsStockPut wmsStockPut) {
        return Repositorys.wmsStockPut.save(WmsStockPutFactory.initBaseFields(wmsStockPut));
    }

    @Override
    public Page<WmsStockPut> page(MultiValueMap<String, String> params) {
        Page<WmsStockPut> wmsStockPutPage = Repositorys.wmsStockPut.findAll(
                WmsStockPutSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return wmsStockPutPage;
    }


    @Override
    public WmsStockPut modify(WmsStockPut wmsStockPutNew) {
        return Repositorys.wmsStockPut.saveAndFlush(WmsStockPutFactory.initBaseFields(wmsStockPutNew));
    }

    @Override
    public Optional<WmsStockPut> single(UUID uuid) {
        Optional<WmsStockPut> wmsStockPut = Repositorys.wmsStockPut.findById(uuid);
        wmsStockPut.get().setStockDetailList(Repositorys.wmsStockDetail.findAll().stream().filter(wmsStockDetail -> wmsStockDetail.getUpper().equals(wmsStockPut.get().getUuid())).collect(Collectors.toList()));
        return wmsStockPut;
    }

    @Override
    public List<WmsStockPut> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsStockPut.findAllById(uuids);
    }

    @Override
    public List<WmsStockPut> delete(List<WmsStockPut> wmsStockPutList) {
        wmsStockPutList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsStockPutFactory.initBaseFields(item);
        });
        return Services.wmsStockPutTransactional.batchModify(wmsStockPutList);
    }

}
